using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using OracleInternal.Common;
using OracleInternal.Network;

namespace Oracle.ManagedDataAccess.Client
{
	public sealed class OracleDataSourceEnumerator : DbDataSourceEnumerator
	{
		private const string DATA_SOURCE = "DataSource";

		private const string INSTANCE_NAME = "InstanceName";

		private const string SERVER_NAME = "ServerName";

		private const string SERVICE_NAME = "ServiceName";

		private const string PROTOCOL = "Protocol";

		private const string PORT = "Port";

		public override DataTable GetDataSources()
		{
			if (ProviderConfig.m_bTraceLevelPublic)
			{
				Trace.Write(OracleTraceLevel.Public, OracleTraceTag.Entry, OracleTraceClassName.OracleDataSourceEnumerator, OracleTraceFuncName.GetDataSources);
			}
			DataTable dataTable = null;
			try
			{
				dataTable = new DataTable("DataSource");
				dataTable.Columns.Add(new DataColumn("InstanceName", typeof(string)));
				dataTable.Columns.Add(new DataColumn("ServerName", typeof(string)));
				dataTable.Columns.Add(new DataColumn("ServiceName", typeof(string)));
				dataTable.Columns.Add(new DataColumn("Protocol", typeof(string)));
				dataTable.Columns.Add(new DataColumn("Port", typeof(string)));
				ProviderConfig.RefreshDataSources();
				foreach (Hashtable item in AddressResolution.NamingAdapterMaps())
				{
					if (item == null)
					{
						continue;
					}
					foreach (string key in item.Keys)
					{
						if (dataTable.Select("InstanceName='" + key + "'").Length != 0)
						{
							continue;
						}
						DataRow dataRow = dataTable.NewRow();
						try
						{
							string value = key;
							string nvString = (string)item[key];
							dataRow["InstanceName"] = value;
							NVPair nVPair = NVNavigator.FindNVPairRecurse(NVFactory.CreateNVPair(nvString), AddressResolution.ADDRESS);
							if (nVPair != null)
							{
								NVPair nVPair2 = NVNavigator.FindNVPair(nVPair, AddressResolution.PROTOCOL);
								if (nVPair2 != null)
								{
									dataRow["Protocol"] = nVPair2.Atom;
								}
								nVPair2 = NVNavigator.FindNVPair(nVPair, AddressResolution.HOST);
								if (nVPair2 != null)
								{
									dataRow["ServerName"] = nVPair2.Atom;
								}
								nVPair2 = NVNavigator.FindNVPair(nVPair, AddressResolution.PORT);
								if (nVPair2 != null)
								{
									dataRow["Port"] = nVPair2.Atom;
								}
							}
							NVPair nVPair3 = NVNavigator.FindNVPairRecurse(NVFactory.CreateNVPair(nvString), AddressResolution.CONNECT_DATA);
							if (nVPair3 != null)
							{
								NVPair nVPair2 = NVNavigator.FindNVPair(nVPair3, AddressResolution.SERVICE_NAME);
								if (nVPair2 != null)
								{
									dataRow["ServiceName"] = nVPair2.Atom;
								}
								else
								{
									nVPair2 = NVNavigator.FindNVPair(nVPair3, AddressResolution.SID);
									if (nVPair2 != null)
									{
										dataRow["ServiceName"] = nVPair2.Atom;
									}
								}
							}
							dataTable.Rows.Add(dataRow);
						}
						catch (Exception)
						{
						}
					}
				}
				return dataTable;
			}
			catch (Exception ex2)
			{
				OracleException.HandleError(OracleTraceLevel.Public, OracleTraceTag.Error, OracleTraceClassName.OracleDataSourceEnumerator, OracleTraceFuncName.GetDataSources, ex2);
				throw;
			}
			finally
			{
				if (ProviderConfig.m_bTraceLevelPublic)
				{
					Trace.Write(OracleTraceLevel.Public, OracleTraceTag.Exit, OracleTraceClassName.OracleDataSourceEnumerator, OracleTraceFuncName.GetDataSources);
				}
			}
		}
	}
}
